﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.Post.BBS
{
    public partial class Plate : Base.BasePage
    {
        public DataSet dsPosts = new DataSet();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        protected void Page_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();
            WriteBackScript("alert('" + ex.Message + "');window.returnValue=true;window.close();");
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        protected void BindData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["plateID"]))
            {
                int id = 0;

                if (!int.TryParse(Request.QueryString["plateID"], out id))
                {
                    throw new Exception("你所访问的页面不存在!");
                }

                ExtendBLL.PostPlate pp = new ExtendBLL.PostPlate();
                LabMS.Model.PostPlate ppInfo = pp.GetModel(id);

                if (ppInfo == null)
                {
                    throw new Exception("当前版块已经在另一处被删除!");
                }

                ExtendBLL.Post post = new ExtendBLL.Post();
                DataSet dsWholePosts = post.GetAllWithFullInfo(ppInfo.ID);

                AspNetPager.RecordCount = dsWholePosts.Tables[0].Rows.Count;

                HandleData(dsWholePosts);
            }
            else
            {
                throw new Exception("你所访问的页面不存在!");
            }
        }

        /// <summary>
        /// 处理数据
        /// </summary>
        /// <param name="ds"></param>
        public void HandleData(DataSet ds)
        {
            int count = ds.Tables[0].Rows.Count;
            int current = AspNetPager.PageSize * (AspNetPager.CurrentPageIndex - 1) + 1;

            dsPosts.Tables.Add("table");
            dsPosts.Tables[0].Columns.AddRange(new DataColumn[] { 
                new DataColumn("ID"),
                new DataColumn("PostTitle"),
                new DataColumn("PosterID"),
                new DataColumn("PostContent"),
                new DataColumn("PostRecordTime"),
                new DataColumn("PostReplyCount"),
                new DataColumn("PosterType"),
                new DataColumn("PostPlateID"),
                new DataColumn("ViewTimes")
            });

            DataRow row;
            for (int i = current - 1; (i < current + AspNetPager.PageSize - 1) && (i < count); i++)
            {
                row = ds.Tables[0].Rows[i];

                dsPosts.Tables[0].Rows.Add(row["ID"], row["PostTitle"], row["PosterID"], row["PostContent"], row["PostRecordTime"],
                    row["PostReplyCount"], row["PosterType"], row["PostPlateID"], row["ViewTimes"]);
            }
        }


        /// <summary>
        /// 写回脚本
        /// </summary>
        /// <param name="script">脚本内容</param>
        protected void WriteBackScript(string script)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
                Guid.NewGuid().ToString(), "<script type='text/javascript'>" + script + "</script>");
        }

        protected void AspNetPager_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

    }
}
